package com.bdsoft.actions.noticeManage.Controller;

import com.bdsoft.actions.AcUrls;
import com.bdsoft.actions.noticeManage.DTO.NoticeDTO;
import com.bdsoft.actions.noticeManage.Service.NoticeService;
import com.bdsoft.security.util.JwtUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(AcUrls.NoticeManage.noticeManageUrl)
@Tag(name = "通知公告管理")
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService noticeService;

    /**
     * 获取公告列表接口
     * 根据页码、每页大小、标题、状态等条件获取公告列表的分页信息
     *
     * @param pageNum 请求参数，页码，默认为1
     * @param pageSize 请求参数，每页大小，默认为10
     * @param title 请求参数，标题，非必填
     * @param status 请求参数，状态，非必填
     * @param authHeader 请求头，认证信息
     * @return 返回响应实体，包含公告列表分页信息
     */
    @GetMapping(AcUrls.NoticeManage.getNoticeList)
    @Operation(summary = "获取公告列表")
    public ResponseEntity<?> getNoticeList(
            @RequestParam(defaultValue = "1") int pageNum, // 请求参数，页码，默认为1
            @RequestParam(defaultValue = "10") int pageSize, // 请求参数，每页大小，默认为10
            @RequestParam(required = false) String title, // 请求参数，标题，非必填
            @RequestParam(required = false) Integer status, // 请求参数，状态，非必填
           @RequestHeader("Authorization") String authHeader // 请求头，认证信息
    ) {

        // 从请求头中获取Authorization，并去掉"Bearer "前缀
        String token = authHeader.substring(7); // 去掉"Bearer "前缀

        // 从token中获取用户名
        String username = JwtUtil.getUsernameFromToken(token);

        // 从token中获取用户ID
        String userId = JwtUtil.getUserIdFromToken(token);

        // 调用服务层方法，获取公告列表分页信息
        PageInfo<Map<String, Object>> pageInfo = noticeService.getNoticeList(pageNum, pageSize, title, status, authHeader);



        // 返回响应实体，包含公告列表分页信息
        return ResponseEntity.ok(pageInfo);
    }


    /**
     * 创建公告接口
     * 该接口用于创建新的公告，必须提供唯一ID，并通过请求头传递授权信息。
     *
     * @param noticeDTO 公告数据传输对象，包含公告的详细信息
     * @param authHeader 授权头信息，用于验证用户身份
     * @return 返回创建公告的结果
     */
    @PostMapping(AcUrls.NoticeManage.createNotice)
    @Operation(summary = "创建公告", description = "必须提供唯一ID")
    public ResponseEntity<?> createNotice(@RequestBody @Valid NoticeDTO noticeDTO, @RequestHeader("Authorization") String authHeader) {
        // 调用noticeService的createNotice方法创建公告
        // noticeService.createNotice方法需要两个参数：公告数据传输对象noticeDTO和授权头信息authHeader
        // 方法返回创建公告的结果
        return ResponseEntity.ok(noticeService.createNotice(noticeDTO, authHeader));
    }


    @PostMapping(AcUrls.NoticeManage.updateNotice)
    @Operation(summary = "更新公告")
    public ResponseEntity<?> updateNotice(@RequestBody @Valid NoticeDTO noticeDTO) {
        return ResponseEntity.ok(noticeService.saveOrUpdateNotice(noticeDTO));
    }

    @PostMapping(AcUrls.NoticeManage.deleteNotice)
    @Operation(summary = "删除公告")
    public ResponseEntity<?> deleteNotice(@RequestParam String id) {
        return ResponseEntity.ok(noticeService.deleteNotice(id));
    }

    @PostMapping(AcUrls.NoticeManage.batchDeleteNotice)
    @Operation(summary = "批量删除公告")
    public ResponseEntity<?> batchDeleteNotice(@RequestBody List<String> ids) {
        return ResponseEntity.ok(noticeService.batchDeleteNotice(ids));
    }

    @GetMapping(AcUrls.NoticeManage.getNoticeDetail)
    @Operation(summary = "获取公告详情")
    public ResponseEntity<?> getNoticeDetail(@RequestParam String id) {
        return ResponseEntity.ok(noticeService.getNoticeDetail(id));
    }

    @PostMapping(AcUrls.NoticeManage.publishNotice)
    @Operation(summary = "发布公告")
    public ResponseEntity<?> publishNotice(@RequestParam String id) {
        return ResponseEntity.ok(noticeService.publishNotice(id));
    }

    @PostMapping(AcUrls.NoticeManage.revokeNotice)
    @Operation(summary = "撤回公告")
    public ResponseEntity<?> revokeNotice(@RequestParam String id) {
        return ResponseEntity.ok(noticeService.revokeNotice(id));
    }
}